<template>
	<view class="timer">
		{{ti}}
	</view>
</template>
<script>
export default {
	data() {
		return {
			ti:'00:00:00',
			timer: '',
			hour: 0,
			minutes: 0,
			seconds: 0
		};
	},

	destroyed() {
		clearInterval(this.timer);
	},
	methods: {
		// 开始计时
		begin(){
			console.log('开始计时')
			this.timer = setInterval(this.startTimer, 1000);	
		},
		startTimer() {
			this.seconds += 1;
			if (this.seconds >= 60) {
				this.seconds = 0;
				this.minutes = this.minutes + 1;
			}
			if (this.minutes >= 60) {
				this.minutes = 0;
				this.hour = this.hour + 1;
			}
			this.ti= (this.hour < 10 ? '0' + this.hour : this.hour) + ':' + (this.minutes < 10 ? '0' + this.minutes : this.minutes) + ':' + (this.seconds < 10 ? '0' + this.seconds : this.seconds);
		},
		// 暂停倒计时
		pause() {
			if(this.timer) {
				clearInterval(this.timer);
				// this.timer = null
			}
		},
	}
};
</script>
<style></style>

